<template>
  <div>
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>关键词：</label>
            <el-input type="text" clearable placeholder="请输入关键词" v-model="keyword"></el-input>
          </div>
          <div class="operate_item">
            <label>状态：</label>
            <el-select filterable clearable v-model="status" placeholder="请选择状态">
              <el-option
                v-for="(item,index) in statusOptions"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span class="operate_btn search_btn" @click="openPopup('',1)">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="80"
            prop="id"
            label="ID">
          </el-table-column>
          <el-table-column
            sortable
            prop="modifyTime"
            width="160"
            label="修改时间">
            <template slot-scope="scope">
              <p style="text-align: left">{{formatTime(scope.row.modifyTime)}}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="名称">
          </el-table-column>
          <el-table-column
            label="备注">
            <template slot-scope="scope">
              <span>{{scope.row.memo?scope.row.memo:'——'}}</span>
            </template>
          </el-table-column>
          <el-table-column
            width="120"
            label="状态" v-if="!(btn.freeze||btn.unfreeze)">
            <template slot-scope="scope">
              <span class="normal" :class="{'unusual':scope.row.enable===0}">{{scope.row.enable?'可用':'冻结'}}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="状态"
            prop="2"
            sortable="custom"
            width="100" v-if="btn.freeze||btn.unfreeze">
            <template slot-scope="scope">
              <div>
                <el-switch
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="deleteItem(scope.row,scope.$index)"
                  v-model="scope.row.enable">
                </el-switch>
                <span class="normal" v-if="scope.row.enable===1">可用</span>
                <span class="unusual" v-else>冻结</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            fixed="right"
            v-if="btn&&(btn.update||btn.delete||btn.assignLimit)"
            width="200">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="openPopup(scope.row,1)" v-if="btn.update">编辑</span>
                <span class="change_info" @click="openPopup(scope.row,2)" v-if="btn.assignLimit">分配权限</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{totals}}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <edit v-if="isPop" :rid="roleId" @listenToChild="listenToEdit"></edit>

   <!-- 分配权限的组件 -->
    <el-dialog :title="roleName+'-权限分配'" :visible.sync="isLog" width="70%" @close='isLog=false' append-to-body v-dialogDrag>
      <limitTree :roleId="roleId" v-if="isLog" @listenToChild="isLog=false"></limitTree>
    </el-dialog>
  </div>
</template>

<script>

import edit from "./edit";
import limitTree from "./limitTree";
import commons from "@/utils/common";
import {getBtn} from "@/utils/btnAuth";
import {listData,delData} from "@/api/sys/role";

export default {
  components:{
    limitTree,
    edit
  },
  data () {
    return {
      keyword:'',
      status:'',
      statusOptions: [{id: '', name: '全部'}, {id: 0, name: '冻结'}, {id: 1, name: '启用'}],
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo: [],
      isPop:false,
      roleId:'',
      roleName:'',
      isLog:false,
      btn:{}
    }
  },
  mounted() {
    this.btn = getBtn();
    if (this.btn.query){
      this.getTableInfo(1);
    }
  },
  methods:{
    // 获取列表信息
    getTableInfo(page){
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit:that.pageSize,
        page:page,
        name:that.keyword,
        enable:that.status
      }
      listData(queryParams).then(res => {
        that.tableInfo = res?res.list:[];
        that.totals = res?res.count:0;
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    resetTable() {
      this.keyword = '';
      this.status = '';
      this.pageSize = 15;
      this.getTableInfo(1);
    },

    // 打开弹窗
    openPopup(obj,type){
      type===1?this.isPop = true:this.isLog = true;
      this.roleId = obj.id;
      this.roleName = obj.name;
    },

    listenToEdit(e){
      this.isPop = false;
      this.roleName = '';
      if (e.refresh){
        this.getTableInfo(this.currentPage)
      }
    },

    // 移除
    deleteItem(obj,index) {
      let that = this;
      that.$confirm('确定'+(obj.enable===1?'解冻':'冻结')+obj.name+'?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {id:obj.id,enable:obj.enable}
        return delData(param);
      }).then(() => {
        that.getTableInfo(that.currentPage);
        that.$message.success({
          message:'修改成功！',
          duration:1500
        });
      }).catch(() => {
        //重置
        that.tableInfo[index].enable = obj.enable == 0 ? 1:0;
      });
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600;'
    },
  }
}
</script>

<style scoped>
.check_btn{
  color: #f7ab20;
}
</style>
